<?php
class FiveChess extends Zend_Db_Table
{
	protected $_name ="fivechess_gaming";
	protected $_primary = 'id'; 
  public function regretChessboard($chessboard)	{
  	$ret = "";
  	$tmparray = explode("|", $chessboard);
  	if(sizeof($tmparray) > 2)	{
	  	array_pop($tmparray);
	  	array_pop($tmparray);
	  	/*
	  	for($i = 0; $i < sizeof($tmparray) -2 ; $i++)	{
	  		if($i == sizeof($tmparray) -3)
	  			$ret .= $tmparray[$i];
	  		else
	  			$ret .= $tmparray[$i]."|";
	  	}
	  	*/
  	}
	  $ret = join("|", $tmparray);
	  echo $ret;
		//$chessboard |= $val << 4 * ($step_no - 1);
		return $ret;
	} 
	
  public function setChessboardVal($chessboard, $val, $step_no)	{
  	$ret = "";
  	if($step_no < 1)	return $ret;
  	if($step_no == 1)
  		$ret = $val;
  	else
  		$ret = $chessboard."|".$val;
		//$chessboard |= $val << 4 * ($step_no - 1);
		return $ret;
	}
	
  public function setUser_ID_Zero($user_type, $user_id)	{
		$db = $this->getAdapter();
  	$data=array(
	    		$user_type.'_id'=>0
					);
		$where = $db->quoteInto($user_type.'_id = ?', $user_id);
		$this->update($data, $where);
	}
	
	public function getGameRoom()	{
		$GameRoom = $this->fetchAll()->toArray();
		return $GameRoom;
	}
	
	public function getGameRoomOther($id)	{
		$GameRoom = $this->fetchAll('id !='.$id)->toArray();
		return $GameRoom;
	}
	
	public function GetRoomGamingList($room_id)
	{
		$where = $this->getAdapter()->quoteInto('room_id = ?', $room_id);
		$gamingRowSet = $this->fetchAll($where);
		return $gamingRowSet->toArray();
	}
	
//	public function getRoom_join_User($id)	{
//		$db = $this->getadapter();
//		
//		$Room_User = $db->query(
//		'select fivechess_gaming.id,host_id,user1.username as host_name,user1.face as host_face,
//		guest_id,user2.username as guest_name,user2.face as guest_face,fivechess_gaming.status from fivechess_gaming ,
//		auth_user as user1,auth_user as user2 where (host_id = user1.id and guest_id = user2.id) 
//		or (host_id = 0 and guest_id = 0 and user1.id = 1 and user2.id =1) or (host_id = 0 and 
//		guest_id = user2.id and user1.id = 1) or (host_id = user1.id and 
//		guest_id = 0 and user2.id = 1);');
//		$row = $Room_User->fetchAll();
//		return $row;
//
//	}
	public function GetRoomGaming($room_id)
	{
		$where = $this->getAdapter()->quoteInto('room_id = ?', $room_id);
		$gamingRowSet = $this->fetchAll($where);
		return $gamingRowSet;
	}
	
	public function getChessBordByID($id)	{
		$ChessBord = $this->fetchRow('id ='. $id)->toArray();
		return $ChessBord;
	}
	
	public function updateChessBordByID($data, $id)	{
		if(isset($data))	{
			$db = $this->getAdapter();
			$where = $db->quoteInto('id = ?', $id);
			$this->update($data, $where);
		}
	}
	
	public function createChessBord($data){
		$db = $this->getAdapter();
		$this->insert($data);
		$db->lastInsertId();
		return true;
	}
	
	public function delChessBordByID($id){
		 $where = 'id = ' . $id;
         $this->delete($where);
         return true;
	}
	
	function countByID($id = 0)	{
		$count = count($this->fetchRow('id='.$id));
		return $count;
	}
	
	function countAll()	{
		$db = $this->getAdapter();
    $select = $db->select();
    $select->from($this->_name, 'id');
    $count = count($db->fetchAll($select));
    if($count)
    {
          return $count;
    }
    return 0;
	}
	

}

